var Fly = require('flyio/dist/npm/wx')
var fly = new Fly()
fly.config = {
    method: 'POST', //请求方法， GET 、POST ...
    headers: {
        // Authorization:'Bearer' +'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjk5NzMsImlzcyI6Imh0dHA6Ly90ZXN0LmFwaTIuYW1laW1laWthLmNvbS9hcGkvYXV0aC9sb2dpbiIsImlhdCI6MTUzNzI2MTAxMSwiZXhwIjoxNTU4ODYxMDExLCJuYmYiOjE1MzcyNjEwMTEsImp0aSI6InhjQ2czZFdraE9EazZpdkgifQ.DUbx6tX404Dg9wk0wReyWhECSLag2KLWOxtSqlr4Cao'
        Authorization: '',
        'content-type': 'application/x-www-form-urlencoded',
        appVersion: '2.0',
        appSystem: 'minapp'
    },
    // 请求头
    // baseURL: 'https://app.ameimeika.com/', //请求基地址
    //   baseURL: 'https://app2.ameimeika.com/', //请求基地址（预上线服）
    baseURL: "https://mp.ameimeika.com/", //请求基地址（测试服）
    //是否自动将Content-Type为“application/json”的响应数据转化为JSON对象，默认为true
    parseJson: true,
    timeout: '10000' //超时时间
}
fly.interceptors.request.use(request => {
    request.timeout = 30000
    if (wx.getStorageSync('user_token')) {
        //检查本地缓存是否有token存在没有则重新获取
        request.headers = {
            //设置请求头
            Authorization: 'Bearer ' + wx.getStorageSync('user_token'),
            'content-type': 'application/x-www-form-urlencoded',
            appVersion: '2.0',
            appSystem: 'minapp'
        }
        return request
    }
})
fly.interceptors.response.use(
    response => {
        //只将请求结果的data字段返回
        let res = response.data
        if (res.status_code == 1017) {
            wx.showToast({
                title: res.message, //提示的内容,
                icon: 'none', //图标,
                duration: 1500, //延迟时间,
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => {
                    setTimeout(() => {
                        wx.navigateTo({
                            url: '/pages/package_mine/bind_tel/main'
                        })
                    }, 1500)
                }
            })
            return
        }
    },
    err => {
        //请求出错，根据返回状态码判断出错原因
        // Do something with response error
    }
)

export default fly
